﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LiteBlog.Controllers.Extensions;
using LiteBlog.Models;

namespace LiteBlog.Controllers.Filters
{
    public class ValidateFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var controller = filterContext.Controller as BaseController;
            if (!controller.ModelState.IsValid)
            {
                var resultModel = new ResultModel();
                resultModel.Result = false;
                resultModel.Message = "Please correct the following errors:";
                resultModel.Errors = controller.ModelState.Errors();
                filterContext.Result = controller.GetJsonResult(resultModel);
            }

            base.OnActionExecuting(filterContext);
        }
    }
}